package generators;

import framework.Color;
import gamebackend.Block;
import gamebackend.EnviroLaser;
import gui.BlockStyle;

/**
 * To be used for an EnviroLaser.  Removes path from
 * areas that will be destroyed by the laser,
 * and removes blocks in the wake of the laser's destruction.
 * @author Jonathan Caddey
 */
public class LaserGenerator implements BlockGenerator {
	
	/**
	 * The first column to be affected by the laser.
	 */
	private final int my_left;
	/**
	 * The last column to be affected by the laser.
	 */
	private final int my_right;
	
	/**
	 * The number of rows that will be destroyed.
	 */
	private final int my_destroyedRows;
	/**
	 * The number of rows that will not spawn,
	 * because they were destroyed by the laser.
	 */
	private final int my_wakeRows;
	/**
	 * The rows to skip before activating.
	 */
	private final int my_delayRows;
	private int my_row;
	
	private final double my_preferredLength;
	private final Block my_block = new Block(BlockStyle.PLAIN, Color.RED);
	

	public LaserGenerator(final double the_blockWidth,
			final double the_blockHeight, final EnviroLaser the_laser,
			final double the_delay) {
		my_left = (int)(the_laser.getLeft() / the_blockWidth);
		my_right = (int)((the_laser.getWidth() + the_laser.getLeft()) / the_blockWidth);
		my_destroyedRows = (int)(the_laser.getDestructionLength() / the_blockHeight) - 2;
		my_wakeRows = my_destroyedRows / 2;
		my_delayRows = (int)(the_delay / the_blockHeight);
		my_preferredLength = (my_wakeRows + my_destroyedRows + my_delayRows) * the_blockHeight;
	}

	@Override
	public void generate(Block[][] blocks) {
		
		if (my_row > my_destroyedRows + my_wakeRows + my_delayRows) {
			return;
		}
		if (my_row < my_delayRows) {
			my_row++;
			return;
		}
		if (my_row < my_destroyedRows + my_delayRows) {
			int row;
			if (my_row > blocks.length + my_delayRows){
				row = blocks.length - 1;
			} else {
				row = 0;
			}
			for (; row < blocks.length; row++) {
				for (int col = Math.max(0, my_left);
						col <= my_right && col < blocks[0].length; col++) {
					if (blocks[row][col].isPath()) {
						blocks[row][col] = Block.EMPTY_BLOCK;
					}
//					blocks[row][col] = my_block;
				}
			}
		} else {
			int row = 0;
			for (int col = Math.max(0, my_left);
					col <= my_right && col < blocks[0].length; col++) {
				blocks[row][col] = Block.EMPTY_BLOCK;
			}
		}
		my_row++;
	}
	/**
	 * How long this generator would like to be alive.
	 */
	public double getPreferredLength() {
		return my_preferredLength;
	}

}
